﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Http.Extensions;
using Kugar.Core.Web;
#endif
using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Helpers;


namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons
{
    public class PageCommon_PopupWindow:ContainerBase
    {
        private string _title = "";
        private PopupShowSide _side = PopupShowSide.RightToLeft;
        private ButtonItemBase _titleButton = null;

        public PageCommon_PopupWindow(ViewContext viewContext) : base(viewContext)
        {
            this.Css("weui-popup__container popup-bottom");
        }

        /// <summary>
        /// 标题
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public PageCommon_PopupWindow Title(string title)
        {
            _title = title;

            return this;
        }

        /// <summary>
        /// 显示的方向
        /// </summary>
        /// <param name="side"></param>
        /// <returns></returns>
        public PageCommon_PopupWindow ShowSide(PopupShowSide side = PopupShowSide.RightToLeft)
        {
            _side = side;

            return this;
        }

        /// <summary>
        /// 添加一个按钮
        /// </summary>
        /// <param name="configFunc"></param>
        /// <returns></returns>
        public PageCommon_PopupWindow TitleButton(Action<ButtonItemBase> configFunc)
        {
            var config = new ButtonItemBase();
            config.Class("picker-button");

            configFunc(config);

            _titleButton = config;

            return this;
        }

        public override void Dispose()
        {
            ViewContext.Writer.WriteLine("</div> <!-- end  modal-content -->");

            ViewContext.Writer.WriteLine("</div> <!-- end popup__modal -->");

            ViewContext.Writer.WriteLine("</div> <!-- end popup__container -->");

        }

        internal override ContainerBase RenderInternal()
        {
            var sideCss = "";

            if (_side != PopupShowSide.BottomToTop)
            {
                switch (_side)
                {
                    case PopupShowSide.TopToBottom:
                        sideCss = "fromTop";
                        break;
                    case PopupShowSide.LeftToRight:
                        sideCss = "fromLeft";
                        break;
                    case PopupShowSide.RightToLeft:
                        sideCss = "fromRight";
                        break;
                }

                this.Css(sideCss);
            }

#if NETCOREAPP
            _attrs.Data("url", ViewContext.HttpContext.Request.GetDisplayUrl());
           
#endif
#if !NETCOREAPP
      _attrs.Data("url", ViewContext.RequestContext.HttpContext.Request.RawUrl);
#endif



            ViewContext.Writer.WriteLine($"<div {_attrs}>");
            ViewContext.Writer.WriteLine("<div class=\"weui-popup__overlay\"></div>");
            ViewContext.Writer.WriteLine("<div class=\"weui-popup__modal\">");

            if (!string.IsNullOrWhiteSpace(_title) || _titleButton!=null)
            {
                var titleButtonHtml = "";

                if (_titleButton==null)
                {
                    titleButtonHtml =
                        "<a href=\"javascript:;\" class=\"picker-button close-popup\">关闭</a>";
                }
                else
                {
                    var href = "";
                    var onclick = "";

                    if (!string.IsNullOrWhiteSpace(_titleButton._href))
                    {
                        href = $"href=\"{_titleButton.getHref}\"";
                    }

                    if (!string.IsNullOrWhiteSpace(_titleButton.getOnClickScript))
                    {
                        onclick = $"onclick=\"{_titleButton.getOnClickScript}\"";
                    }

                    titleButtonHtml = $"<a {_titleButton.Attributes} {href} {onclick}>{_titleButton.getText}</a>";//  Utility.ButtonConfigToHtml(_titleButton,"a");
                }

                ViewContext.Writer.WriteLine($"<div class=\"toolbar\"><div class=\"toolbar-inner\">{titleButtonHtml}<h1 class=\"title\">{_title}</h1></div></div>");
            }

            ViewContext.Writer.WriteLine($"<div class=\"modal-content\" >");

            return this;
        }
    }
}